package constant.milk.cashbook.widget;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;

import constant.milk.cashbook.R;
import constant.milk.cashbook.auto.AutoManager;
import constant.milk.cashbook.bean.BankData;
import constant.milk.cashbook.bean.CheckData;
import constant.milk.cashbook.bean.CreditData;
import constant.milk.cashbook.bean.GiftData;
import constant.milk.cashbook.bean.InOutData;
import constant.milk.cashbook.bean.PhoneData;
import constant.milk.cashbook.bean.RealData;
import constant.milk.cashbook.bean.WidgetSettingData;
import constant.milk.cashbook.database.DBSelectQuery;
import constant.milk.cashbook.database.DbAdapter;
import constant.milk.cashbook.util.MilkManager;
import constant.milk.cashbook.util.MilkUtil;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.RemoteViews;

public class MintWidget extends AppWidgetProvider
{
	private static final int REQUET_WIDGET_1 = 0;
	private static final int REQUET_WIDGET_2 = 1;
	private static final int REQUET_WIDGET_3 = 2;
	
	private static final int WIDGET_UPDATE_INTERVAL = 60000;
	private static PendingIntent mSender;  
	private static AlarmManager mManager;
	
	public static final String MINT_RECORDE = "constant.milk.cashbook.widget.MINT_RECORDE";
	public static final String MINT_SELECT = "constant.milk.cashbook.widget.MINT_SELECT";
	public static final String MINT_PRE = "constant.milk.cashbook.widget.MINT_PRE";
	public static final String MINT_NEXT = "constant.milk.cashbook.widget.MINT_NEXT";
	
	private int year;
	private int month;
	private int day;
	
	private int year1;
	private int month1;
	private int day1;
	
	private int year2;
	private int month2;
	private int day2;
	
	private DBSelectQuery select;
	
	private MilkManager milkManager;
	
	private int dateKind;
	
	private int startDate;
	
	private WidgetSettingData data1;
	
	private WidgetSettingData data2;
	
	private WidgetSettingData data3;
	
	private RemoteViews remoteView;
	
	
	@Override
	public void onReceive(Context context, Intent intent)
	{
		super.onReceive(context, intent);
		
		String action = intent.getAction();
		
		if (action.equals(MINT_RECORDE))
		{
			DbAdapter db = DbAdapter.getInstance();
			
			AutoManager autoManager = AutoManager.getInstance();
			
			String massage = autoManager.start(context, db);
			
			if (!massage.equals(""))
			{
				MilkUtil.showToast(context, massage);
			}
			
			Calendar cal = Calendar.getInstance();
			
			SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
			SharedPreferences.Editor editor = pref.edit();
			
			editor.putInt(context.getString(R.string.prefWidgetYear), cal.get(Calendar.YEAR));
			editor.putInt(context.getString(R.string.prefWidgetMonth), cal.get(Calendar.MONTH));
			editor.putInt(context.getString(R.string.prefWidgetDay), cal.get(Calendar.DAY_OF_MONTH));
			editor.commit();
			
			setUpdate(context);
		}
		else if (action.equals(MINT_SELECT))
		{
			setSelectData(context);
		}
		else if (action.equals(MINT_PRE))
		{
			preDate(context);
		}
		else if (action.equals(MINT_NEXT))
		{
			nextDate(context);
		}
		else if (action.equals("android.appwidget.action.APPWIDGET_UPDATE"))
		{
			setUpdate(context);
			
			if(mManager != null && mSender != null)    
			{      
				mSender.cancel();      
				mManager.cancel(mSender);
			}  
			
			long firstTime = System.currentTimeMillis() + WIDGET_UPDATE_INTERVAL;
			mSender = PendingIntent.getBroadcast(context, 0, intent, 0);
			mManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
			mManager.set(AlarmManager.RTC, firstTime, mSender);
		}
	}
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds)
	{
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		
		setUpdate(context);
	}
	
	private void setUpdate(Context context)
	{
		setInit(context);
		
		setDateData(context);
		
		setData(context);
		
		setOnClicklistener(context);
		
		setPrefData(context);
		
		AppWidgetManager manager = AppWidgetManager.getInstance(context);
		ComponentName componentName = new ComponentName(context, MintWidget.class);
		manager.updateAppWidget(componentName, remoteView);
	}

	private void setInit(Context context)
	{
		DbAdapter db = DbAdapter.getInstance();
		
		milkManager = new MilkManager(context, db);
		
		select = new DBSelectQuery(context, db);
		
		remoteView = new RemoteViews(context.getPackageName(), R.layout.widget_4x2);
		
		SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		startDate = pref.getInt(context.getString(R.string.prefStartDate), 1);
		dateKind = pref.getInt(context.getString(R.string.prefWidgetSelect), 0);
		
		Calendar cal = Calendar.getInstance();
		
		year = pref.getInt(context.getString(R.string.prefWidgetYear), cal.get(Calendar.YEAR));
		month = pref.getInt(context.getString(R.string.prefWidgetMonth), cal.get(Calendar.MONTH));
		day = pref.getInt(context.getString(R.string.prefWidgetDay), cal.get(Calendar.DAY_OF_MONTH));
	}
	
	private void setPrefData(Context context)
	{
		SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = pref.edit();
		
		editor.putInt(context.getString(R.string.prefWidgetSelect), dateKind);
		editor.putInt(context.getString(R.string.prefWidgetYear), year);
		editor.putInt(context.getString(R.string.prefWidgetMonth), month);
		editor.putInt(context.getString(R.string.prefWidgetDay), day);
		
		editor.commit();
	}

	@Override
	public void onEnabled(Context context)
	{
		super.onEnabled(context);
		
		setUpdate(context);
		
//		// 메모리 제거시 죽일 프로세스에 선택되지 않도록 하는 코드
//		PackageManager pm = context.getPackageManager();
//		pm.setComponentEnabledSetting(new ComponentName("constant.milk.cashbook.widget", "WidgetService"),
//				PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
//				PackageManager.DONT_KILL_APP);
	}
	
	@Override
	public void onDeleted(Context context, int[] appWidgetIds)
	{
		super.onDeleted(context, appWidgetIds);
		
		if(mManager != null && mSender != null)    
		{      
			mSender.cancel();      
			mManager.cancel(mSender);
		}  
	}
			
	public void setOnClicklistener(Context context)
	{
		Intent intent1 = new Intent(context, WidgetUpdateActivity.class);
		intent1.putExtra(WidgetUpdateActivity.WIDGET_SETTING_ID, data1.getId());
		PendingIntent pendingIntent1 = PendingIntent.getActivity(context, REQUET_WIDGET_1, 
				intent1, PendingIntent.FLAG_UPDATE_CURRENT);
		remoteView.setOnClickPendingIntent(R.id.widgetIcon1View, pendingIntent1);
		
		Intent intent2 = new Intent(context, WidgetUpdateActivity.class);
		intent2.putExtra(WidgetUpdateActivity.WIDGET_SETTING_ID, data2.getId());
		PendingIntent pendingIntent2 = PendingIntent.getActivity(context, REQUET_WIDGET_2, 
				intent2, PendingIntent.FLAG_UPDATE_CURRENT);
		remoteView.setOnClickPendingIntent(R.id.widgetIcon2View, pendingIntent2);
		
		Intent intent3 = new Intent(context, WidgetUpdateActivity.class);
		intent3.putExtra(WidgetUpdateActivity.WIDGET_SETTING_ID, data3.getId());
		PendingIntent pendingIntent3 = PendingIntent.getActivity(context, REQUET_WIDGET_3, 
				intent3, PendingIntent.FLAG_UPDATE_CURRENT);
		remoteView.setOnClickPendingIntent(R.id.widgetIcon3View, pendingIntent3);
		
		Intent intent4 = new Intent(MintWidget.MINT_RECORDE);
		PendingIntent pendingIntent4 = PendingIntent.getBroadcast(context, 
				0, intent4, PendingIntent.FLAG_UPDATE_CURRENT);
		remoteView.setOnClickPendingIntent(R.id.widgetRenewalView, pendingIntent4);
		
		Intent intent5 = new Intent(MintWidget.MINT_SELECT);
		PendingIntent pendingIntent5 = PendingIntent.getBroadcast(context, 
				0, intent5, PendingIntent.FLAG_UPDATE_CURRENT);
		remoteView.setOnClickPendingIntent(R.id.widgetSelectView, pendingIntent5);
		
		Intent intent6 = new Intent(MintWidget.MINT_PRE);
		PendingIntent pendingIntent6 = PendingIntent.getBroadcast(context, 
				0, intent6, PendingIntent.FLAG_UPDATE_CURRENT);
		remoteView.setOnClickPendingIntent(R.id.widgetPreView, pendingIntent6);
		
		Intent intent7 = new Intent(MintWidget.MINT_NEXT);
		PendingIntent pendingIntent7 = PendingIntent.getBroadcast(context, 
				0, intent7, PendingIntent.FLAG_UPDATE_CURRENT);
		remoteView.setOnClickPendingIntent(R.id.widgetNextView, pendingIntent7);
	}
	
	private void setData(Context context)
	{
		ArrayList<WidgetSettingData> list = select.getWidgetSettingList();
		
		// 위젯 1번
		data1 = list.get(0);
		
		remoteView.setImageViewResource(R.id.widgetIcon1ImageView, getImage(data1.getType()));
		
		if (data1.getAsset().equals("6"))
		{
			remoteView.setTextViewText(R.id.widgetName1TextView, context.getString(R.string.allAsset));
		}
		else 
		{
			if (data2.getName().equals(""))
			{
				remoteView.setTextViewText(R.id.widgetName3TextView, getAssetAll(data2.getAsset(), context));
			}
			else
			{
				remoteView.setTextViewText(R.id.widgetName3TextView, data2.getName());
			}
		}
		
		if (data1.getType().equals("0"))
		{
			remoteView.setTextColor(R.id.widgetMoney1TextView, context.getResources().getColor(R.color.outColor));
		}
		else if (data1.getType().equals("1"))
		{
			remoteView.setTextColor(R.id.widgetMoney1TextView, context.getResources().getColor(R.color.inColor));
		}
		else if (data1.getType().equals("2"))
		{
			remoteView.setTextColor(R.id.widgetMoney1TextView, context.getResources().getColor(R.color.moveColor));
		}
		else if (data1.getType().equals("4"))
		{
			remoteView.setTextColor(R.id.widgetMoney1TextView, context.getResources().getColor(R.color.balanceColor));
		}
		
		remoteView.setTextViewText(R.id.widgetMoney1TextView, MilkUtil.getWon(getMoney(data1)));
		
		// 위젯 2번
		data2 = list.get(1);
		
		remoteView.setImageViewResource(R.id.widgetIcon2ImageView, getImage(data2.getType()));
		
		if (data2.getAsset().equals("6"))
		{
			remoteView.setTextViewText(R.id.widgetName2TextView, context.getString(R.string.allAsset));
		}
		else 
		{
			if (data2.getName().equals(""))
			{
				remoteView.setTextViewText(R.id.widgetName3TextView, getAssetAll(data2.getAsset(), context));
			}
			else
			{
				remoteView.setTextViewText(R.id.widgetName3TextView, data2.getName());
			}
		}
		
		if (data2.getType().equals("0"))
		{
			remoteView.setTextColor(R.id.widgetMoney2TextView, context.getResources().getColor(R.color.outColor));
		}
		else if (data2.getType().equals("1"))
		{
			remoteView.setTextColor(R.id.widgetMoney2TextView, context.getResources().getColor(R.color.inColor));
		}
		else if (data2.getType().equals("2"))
		{
			remoteView.setTextColor(R.id.widgetMoney2TextView, context.getResources().getColor(R.color.moveColor));
		}
		else if (data2.getType().equals("4"))
		{
			remoteView.setTextColor(R.id.widgetMoney2TextView, context.getResources().getColor(R.color.balanceColor));
		}
		
		remoteView.setTextViewText(R.id.widgetMoney2TextView, MilkUtil.getWon(getMoney(data2)));
		
		// 위젯 3번
		data3 = list.get(2);
		
		remoteView.setImageViewResource(R.id.widgetIcon3ImageView, getImage(data3.getType()));
		
		if (data3.getAsset().equals("6"))
		{
			remoteView.setTextViewText(R.id.widgetName3TextView, context.getString(R.string.allAsset));
		}
		else 
		{
			if (data3.getName().equals(""))
			{
				remoteView.setTextViewText(R.id.widgetName3TextView, getAssetAll(data3.getAsset(), context));
			}
			else
			{
				remoteView.setTextViewText(R.id.widgetName3TextView, data3.getName());
			}
		}
		
		if (data3.getType().equals("0"))
		{
			remoteView.setTextColor(R.id.widgetMoney3TextView, context.getResources().getColor(R.color.outColor));
		}
		else if (data3.getType().equals("1"))
		{
			remoteView.setTextColor(R.id.widgetMoney3TextView, context.getResources().getColor(R.color.inColor));
		}
		else if (data3.getType().equals("2"))
		{
			remoteView.setTextColor(R.id.widgetMoney3TextView, context.getResources().getColor(R.color.moveColor));
		}
		else if (data3.getType().equals("4"))
		{
			remoteView.setTextColor(R.id.widgetMoney3TextView, context.getResources().getColor(R.color.balanceColor));
		}
		
		remoteView.setTextViewText(R.id.widgetMoney3TextView, MilkUtil.getWon(getMoney(data3)));
	}
	
	private void preDate(Context context)
	{
		SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		dateKind = pref.getInt(context.getString(R.string.prefWidgetSelect), 0);
		
		Calendar cal2 = Calendar.getInstance();
		
		year = pref.getInt(context.getString(R.string.prefWidgetYear), cal2.get(Calendar.YEAR));
		month = pref.getInt(context.getString(R.string.prefWidgetMonth), cal2.get(Calendar.MONTH));
		day = pref.getInt(context.getString(R.string.prefWidgetDay), cal2.get(Calendar.DAY_OF_MONTH));
		
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		
		if (dateKind == 0)
		{
			cal.add(Calendar.DAY_OF_MONTH, -1);
		}
		else if (dateKind == 1)
		{
			cal.add(Calendar.MONTH, -1);
		}
		else if (dateKind == 2)
		{
			cal.add(Calendar.YEAR, -1);
		}
		
		year = cal.get(Calendar.YEAR);
		month = cal.get(Calendar.MONTH);
		day = cal.get(Calendar.DAY_OF_MONTH);
		
		SharedPreferences.Editor editor = pref.edit();
		
		editor.putInt(context.getString(R.string.prefWidgetYear), year);
		editor.putInt(context.getString(R.string.prefWidgetMonth), month);
		editor.putInt(context.getString(R.string.prefWidgetDay), day);
		
		editor.commit();
		
		setUpdate(context);
	}
	
	private void nextDate(Context context)
	{
		SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		dateKind = pref.getInt(context.getString(R.string.prefWidgetSelect), 0);
		
		Calendar cal2 = Calendar.getInstance();
		
		year = pref.getInt(context.getString(R.string.prefWidgetYear), cal2.get(Calendar.YEAR));
		month = pref.getInt(context.getString(R.string.prefWidgetMonth), cal2.get(Calendar.MONTH));
		day = pref.getInt(context.getString(R.string.prefWidgetDay), cal2.get(Calendar.DAY_OF_MONTH));
		
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		
		if (dateKind == 0)
		{
			cal.add(Calendar.DAY_OF_MONTH, 1);
		}
		else if (dateKind == 1)
		{
			cal.add(Calendar.MONTH, 1);
		}
		else if (dateKind == 2)
		{
			cal.add(Calendar.YEAR, 1);
		}
		
		year = cal.get(Calendar.YEAR);
		month = cal.get(Calendar.MONTH);
		day = cal.get(Calendar.DAY_OF_MONTH);
		
		SharedPreferences.Editor editor = pref.edit();
		
		editor.putInt(context.getString(R.string.prefWidgetYear), year);
		editor.putInt(context.getString(R.string.prefWidgetMonth), month);
		editor.putInt(context.getString(R.string.prefWidgetDay), day);
		
		editor.commit();
		
		setUpdate(context);
	}
	
	private void setDateData(Context context)
	{
		if (dateKind == 0)
		{
			setToday(context);
		}
		else if (dateKind == 1)
		{
			setMonth(context);
		}
		else if (dateKind == 2)
		{
			setYear(context);
		}
	}
	
	private void setSelectData(Context context)
	{
		SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		dateKind = pref.getInt(context.getString(R.string.prefWidgetSelect), 0);
		
		if (dateKind == 0)
		{
			dateKind = 1;
		}
		else if (dateKind == 1)
		{
			dateKind = 2;
		}
		else if (dateKind == 2)
		{
			dateKind = 0;
		}
		
		SharedPreferences.Editor editor = pref.edit();
		editor.putInt(context.getString(R.string.prefWidgetSelect), dateKind);
		editor.commit();
		
		setUpdate(context);
	}
	
	private void setToday(Context context)
	{
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		
		year1 = cal.get(Calendar.YEAR);
		month1 = cal.get(Calendar.MONTH);
		day1 = cal.get(Calendar.DAY_OF_MONTH);
		
		year2 = cal.get(Calendar.YEAR);
		month2 = cal.get(Calendar.MONTH);
		day2 = cal.get(Calendar.DAY_OF_MONTH);
		
		remoteView.setTextViewText(R.id.widgetSelectTextView, context.getString(R.string.todayBudget));
		
		remoteView.setTextViewText(R.id.widgetDateTextView, 
				year + "년 " + (month+1) + "월 " + day + "일");
		
		dateKind = 0;
	}
	
	private void setMonth(Context context)
	{
		Calendar cal1 = Calendar.getInstance();
		cal1.set(year, month, day);
		
		Calendar cal2 = Calendar.getInstance();
		cal2.set(year, month, day);
		
		if (startDate == 1)
		{
			year1 = cal1.get(Calendar.YEAR);
			month1 = cal1.get(Calendar.MONTH);
			day1 = startDate;
			
			year2 = cal2.get(Calendar.YEAR);
			month2 = cal2.get(Calendar.MONTH);
			day2 = cal2.getActualMaximum(Calendar.DAY_OF_MONTH);
		}
		else if (day >= startDate)
		{
			year1 = cal1.get(Calendar.YEAR);
			month1 = cal1.get(Calendar.MONTH);
			day1 = startDate;
			
			cal2.add(Calendar.MONTH, 1);
			year2 = cal2.get(Calendar.YEAR);
			month2 = cal2.get(Calendar.MONTH);
			day2 = startDate - 1;
		}
		else
		{
			cal1.add(Calendar.MONTH, -1);
			year1 = cal1.get(Calendar.YEAR);
			month1 = cal1.get(Calendar.MONTH);
			day1 = startDate;
			
			year2 = cal2.get(Calendar.YEAR);
			month2 = cal2.get(Calendar.MONTH);
			day2 = startDate - 1;
		}
		
		remoteView.setTextViewText(R.id.widgetSelectTextView, context.getString(R.string.monthBudget));
		
		remoteView.setTextViewText(R.id.widgetDateTextView, 
				year1 + "년 " + (month1+1) + "월 " + "(S : " + startDate + ")");
		
		dateKind = 1;
	}
	
	private void setYear(Context context)
	{
		Calendar cal = Calendar.getInstance();
		cal.set(year, month, day);
		
		year1 = cal.get(Calendar.YEAR);
		month1 = cal.getActualMinimum(Calendar.MONTH);
		day1 = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
		
		year2 = cal.get(Calendar.YEAR);
		month2 = cal.getActualMaximum(Calendar.MONTH);
		day2 = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		
		remoteView.setTextViewText(R.id.widgetSelectTextView, context.getString(R.string.yearBudget));
		
		remoteView.setTextViewText(R.id.widgetDateTextView, 
				year + "년");
		
		dateKind = 2;
	}
	
	private String getMoney(WidgetSettingData data)
	{
		if (data.getType().equals("4"))
		{
			if (data.getAsset().equals("0"))
			{
				if (data.getName().equals(""))
				{
					ArrayList<RealData> list = select.getRealList();
					
					BigDecimal money = BigDecimal.ZERO;
					
					for (int i = 0; i < list.size(); i++)
					{
						money = money.add(new BigDecimal(list.get(i).getMoney()));
					}
					
					return money.toString();
				}
				else
				{
					RealData assetData = select.getRealName(data.getName());
					
					return assetData.getMoney();
				}
			}
			else if (data.getAsset().equals("1"))
			{
				if (data.getName().equals(""))
				{
					ArrayList<BankData> list = select.getBankList();
					
					BigDecimal money = BigDecimal.ZERO;
					
					for (int i = 0; i < list.size(); i++)
					{
						money = money.add(new BigDecimal(list.get(i).getMoney()));
					}
					
					return money.toString();
				}
				else
				{
					BankData assetData = select.getBankName(data.getName());
					
					return assetData.getMoney();
				}
			}
			else if (data.getAsset().equals("2"))
			{
				if (data.getName().equals(""))
				{
					ArrayList<CheckData> list = select.getCheckList();
					
					BigDecimal money = BigDecimal.ZERO;
					
					for (int i = 0; i < list.size(); i++)
					{
						BankData bankData = select.getBankId(list.get(i).getLinkid());
						
						money = money.add(new BigDecimal(bankData.getMoney()));
					}
					
					return money.toString();
				}
				else
				{
					CheckData assetData = select.getCheckName(data.getName());
					BankData bankData = select.getBankId(assetData.getLinkid());
					
					return bankData.getMoney();
				}
			}
			else if (data.getAsset().equals("3"))
			{
				if (data.getName().equals(""))
				{
					ArrayList<CreditData> list = select.getCreditList();
					
					BigDecimal money = BigDecimal.ZERO;
					
					for (int i = 0; i < list.size(); i++)
					{
						money = money.add(new BigDecimal(list.get(i).getMoney()));
					}
					
					return money.toString();
				}
				else
				{
					CreditData assetData = select.getCreditName(data.getName());
					
					return assetData.getMoney();
				}
			}
			else if (data.getAsset().equals("4"))
			{
				if (data.getName().equals(""))
				{
					ArrayList<PhoneData> list = select.getPhoneList();
					
					BigDecimal money = BigDecimal.ZERO;
					
					for (int i = 0; i < list.size(); i++)
					{
						money = money.add(new BigDecimal(list.get(i).getMoney()));
					}
					
					return money.toString();
				}
				else
				{
					PhoneData assetData = select.getPhoneName(data.getName());
					
					return assetData.getMoney();
				}
			}
			else if (data.getAsset().equals("5"))
			{
				if (data.getName().equals(""))
				{
					ArrayList<GiftData> list = select.getGiftList();
					
					BigDecimal money = BigDecimal.ZERO;
					
					for (int i = 0; i < list.size(); i++)
					{
						BigDecimal tempMoney = new BigDecimal(list.get(i).getMoney());
						
						tempMoney = tempMoney.multiply(new BigDecimal(list.get(i).getNumber()));
						
						money = money.add(tempMoney);
					}
					
					return money.toString();
				}
				else
				{
					GiftData assetData = select.getGiftName(data.getName());
					
					BigDecimal money = new BigDecimal(assetData.getMoney());
					
					money = money.multiply(new BigDecimal(assetData.getNumber()));
					
					return money.toString();
				}
			}
			else if (data.getAsset().equals("6"))
			{
				ArrayList<RealData> realList = select.getRealList();
				ArrayList<BankData> bankList = select.getBankList();
				ArrayList<GiftData> giftList = select.getGiftList();
				
				BigDecimal money = BigDecimal.ZERO;
				
				for (int i = 0; i < realList.size(); i++)
				{
					money = money.add(new BigDecimal(realList.get(i).getMoney()));
				}
				
				for (int i = 0; i < bankList.size(); i++)
				{
					money = money.add(new BigDecimal(bankList.get(i).getMoney()));
				}
				
				for (int i = 0; i < giftList.size(); i++)
				{
					BigDecimal giftMoney = new BigDecimal(giftList.get(i).getMoney());
					
					giftMoney = giftMoney.multiply(new BigDecimal(giftList.get(i).getNumber()));
					
					money = money.add(giftMoney);
				}
				
				return money.toString();
			}
		}
		else
		{
			return getSumMoney(data);
		}
		
		return "0";
	}

	private String getSumMoney(WidgetSettingData data)
	{
		ArrayList<InOutData> list = new ArrayList<InOutData>();
		
		if (data.getAsset().equals("3") && dateKind == 1)
		{
			if (data.getName().equals(""))
			{
				ArrayList<CreditData> creditList = select.getCreditList();
				
				for (int i = 0; i < creditList.size(); i++)
				{
					int termDate = Integer.parseInt(creditList.get(i).getTermdate());
					
					ArrayList<InOutData> inOutList = getTermDateInoutData(data, termDate);
					
					list.addAll(inOutList);
				}
			}
			else
			{
				CreditData creditData = select.getCreditName(data.getName());
				
				int termDate = Integer.parseInt(creditData.getTermdate());
				
				list = getTermDateInoutData(data, termDate);
			}
		}
		else if (data.getAsset().equals("4") && dateKind == 1)
		{
			if (data.getName().equals(""))
			{
				ArrayList<PhoneData> phoneList = select.getPhoneList();
				
				for (int i = 0; i < phoneList.size(); i++)
				{
					int termDate = Integer.parseInt(phoneList.get(i).getTermdate());
					
					ArrayList<InOutData> inOutList = getTermDateInoutData(data, termDate);
					
					list.addAll(inOutList);
				}
			}
			else
			{
				PhoneData phoneData = select.getPhoneName(data.getName());
				
				int termDate = Integer.parseInt(phoneData.getTermdate());
				
				list = getTermDateInoutData(data, termDate);
			}
		}
		else
		{
			list  = select.getInOutMainList(
					Integer.parseInt(data.getType()), Integer.parseInt(data.getAsset()), data.getName(), 
					MilkUtil.getDateDB(year1, month1+1, day1), MilkUtil.getDateDB(year2, month2+1, day2));
		}
		
		if (list == null || list.size() == 0)
		{
			return "0";
		}
		
		if (data.getAsset().equals("5"))
		{
			return milkManager.getSumOutGiftCardMoney(list);
		}
		else
		{
			return milkManager.getSumInOutMoney(list, data.getAsset());
		}
	}

	private int getImage(String type)
	{
		if (type.equals("0"))
		{
			return R.drawable.ic_mainout;
		}
		else if (type.equals("1"))
		{
			return R.drawable.ic_mainin;
		}
		else if (type.equals("2"))
		{
			return R.drawable.ic_mainmove;
		}
		else if (type.equals("4"))
		{
			return R.drawable.ic_mainbalance;
		}
		
		return R.drawable.ic_home;
	}
	
	private ArrayList<InOutData> getTermDateInoutData(WidgetSettingData data, int termDate)
	{
		Calendar cal1 = Calendar.getInstance();
		cal1.set(year, month, day);
		
		Calendar cal2 = Calendar.getInstance();
		cal2.set(year, month, day);
		
		int year1;
		int month1;
		int day1;
		
		int year2;
		int month2;
		int day2;
		
		if (termDate == 1)
		{
			year1 = cal1.get(Calendar.YEAR);
			month1 = cal1.get(Calendar.MONTH);
			day1 = 1;
			
			year2 = cal2.get(Calendar.YEAR);
			month2 = cal2.get(Calendar.MONTH);
			day2 = cal2.getActualMaximum(Calendar.DAY_OF_MONTH);
		}
		else if (day >= termDate)
		{
			year1 = cal1.get(Calendar.YEAR);
			month1 = cal1.get(Calendar.MONTH);
			day1 = termDate;
			
			cal2.add(Calendar.MONTH, 1);
			
			year2 = cal2.get(Calendar.YEAR);
			month2 = cal2.get(Calendar.MONTH);
			day2 = termDate - 1;
		}
		else
		{
			cal1.add(Calendar.MONTH, -1);
			
			year1 = cal1.get(Calendar.YEAR);
			month1 = cal1.get(Calendar.MONTH);
			day1 = termDate;
			
			year2 = cal2.get(Calendar.YEAR);
			month2 = cal2.get(Calendar.MONTH);
			day2 = termDate - 1;
		}
		
		return select.getInOutMainList(
					Integer.parseInt(data.getType()), Integer.parseInt(data.getAsset()), data.getName(), 
					MilkUtil.getDateDB(year1, month1+1, day1), MilkUtil.getDateDB(year2, month2+1, day2));
	}
	
	private String getAssetAll(String asset, Context context)
	{
		if (asset.equals("0"))
		{
			return context.getString(R.string.allRealMoney);
		}
		else if (asset.equals("1"))
		{
			return context.getString(R.string.allBankbook);
		}
		else if (asset.equals("2"))
		{
			return context.getString(R.string.allCheckCard);
		}
		else if (asset.equals("3"))
		{
			return context.getString(R.string.allCreditCard);
		}
		else if (asset.equals("4"))
		{
			return context.getString(R.string.allPhone);
		}
		else if (asset.equals("5"))
		{
			return context.getString(R.string.allGiftCard);
		}
		
		return "";
	}
}
